package com.hdey.controller.heng;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.pojo.heng.TbUscDictionary;
import com.hdey.pojo.heng.TbUscPurchaseChild;
import com.hdey.pojo.heng.TbUscPurchaseMaster;
import com.hdey.pojo.max.TbUscStock;
import com.hdey.service.heng.TbUscDictionaryService;
import com.hdey.service.heng.TbUscPurchaseChildService;
import com.hdey.service.heng.TbUscPurchaseMasterService;
import com.hdey.service.max.TbUscStockSvs;
import com.hdey.utils.Result;
import com.hdey.vo.TbUscPurchaseMasterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/heng/purchase")
@Slf4j
public class TbUscPurchaseMasterController {


    @Resource
    private TbUscPurchaseChildService chaseChildService;
    @Resource
    private TbUscPurchaseMasterService chaseMasterService;

    private String msg;

    @Resource
    private TbUscDictionaryService dictionaryService;
    @Resource
    private TbUscStockSvs tbUscStockSvs;



//    @GetMapping("/purchaseIndex")
//    public String purchaseIndex(){
//        return "heng/purchase/index";
//    }

    @GetMapping("/search")
    @ResponseBody
    public Result search(Integer page,Integer limit,String purchaseNo,String purchaseStatus,String supplyCode){
        Page<TbUscPurchaseMaster> list=chaseMasterService.showAll(page,limit,purchaseNo,purchaseStatus,supplyCode);
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(TbUscPurchaseMasterVo masterVo) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(new Date());// new Date()为获取当前系统时间

        SimpleDateFormat dff = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String formatt = dff.format(new Date());// new Date()为获取当前系统时间
        //创建对象
        TbUscPurchaseMaster master=new TbUscPurchaseMaster();
//        master.setPurchaseMasterId(0);
        master.setPurchaseNo(formatt);
        master.setPurchaseStatus("0");
        master.setOrderStatus("0");
        master.setSupplyCode(masterVo.getFactory());
        master.setPrice(masterVo.getTotalPrice());
        master.setRemark(masterVo.getRemark());
        master.setCreateBy("张三");

        int insert = chaseMasterService.insert(master);

        //创建对象
        TbUscPurchaseChild child=new TbUscPurchaseChild();
        child.setPurchaseMasterId(master.getPurchaseMasterId());
        child.setCgCode(masterVo.getCgCode());
        child.setSimplePrice(masterVo.getUnitprice());
        child.setPurchaseBuy(masterVo.getPurchaseBuy());
        child.setResourceDept("仓库");
        child.setResourcePerson("仓库管理员");
        child.setResourceDate(format);
        child.setTotalPrice(masterVo.getTotalPrice());

        int insert1=chaseChildService.insert(child);
        msg="添加成功";
        return Result.success(msg);

    }


    @RequestMapping("/update")
    public Result update(int purchaseMasterId){
        try {
            chaseMasterService.update(purchaseMasterId);
            msg="状态修改成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="状态修改失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/selectId")
    public Result selectId(Integer childId){
        TbUscPurchaseChild child=chaseChildService.selectId(childId);
        System.out.println("子表数据"+child);
        return Result.success(child);
    }

    @PostMapping("/selectByCoCode")
    public Result selectByCoCode(String cgCode){
        TbUscDictionary dictionary=dictionaryService.selectByCgCode(cgCode);
        System.out.println(dictionary);
        return Result.success(dictionary);
    }

    @PostMapping("/addstock")
    public Result addstock(Integer warehousId,Integer amount,String xiaoqi,String cgCode){
        try {
        //根据物资编码获取物资id
        TbUscDictionary dictionary=dictionaryService.selectByCgCode(cgCode);
        Integer dictionaryId=dictionary.getDictionaryId();
        //根据仓库id和物资id判断库存表中是否有数据
        TbUscStock tbUscStock = tbUscStockSvs.countId(warehousId, dictionaryId);
        if(tbUscStock!=null){
            //进行修改
            //修改数量
            Integer amount01=tbUscStock.getAmount();
            Integer zonghe=amount+amount01;
            tbUscStock.setAmount(zonghe);
            tbUscStockSvs.updateAmount(tbUscStock);
        }else {
            //添加数据
            TbUscStock tbUscStock01=new TbUscStock();
            tbUscStock01.setDictionaryId(dictionaryId);
            tbUscStock01.setAmount(amount);
            tbUscStock01.setWarehousId(warehousId);
            tbUscStock01.setXiaoqi(xiaoqi);
            tbUscStockSvs.add(tbUscStock01);
        }
            msg="入库成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="入库失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }
}
